package com.example.stock.service.impl;

import com.example.stock.excel.Finance;
import com.example.stock.excel.Wugu;
import com.example.stock.excel.finance.*;
import com.example.stock.service.FinanceDataService;
import com.example.stock.service.WuguService;
import com.example.stock.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FinanceDataServiceImpl implements FinanceDataService {

    WuguService wuguService;

    @Autowired
    public void setWuguService(WuguService wuguService) {
        this.wuguService = wuguService;
    }

    /**
     * 按股票名称划分数据
     * @return
     */
    public Map<String, List<Finance>> getStockFinanceData() {
        // 按股票名称分组
        return DataUtil.getFinanceData().stream()
                .collect(Collectors.groupingBy(Finance::getStock));
    }

    /**
     * 按季度划分数据
     * @return
     */
    public Map<String, List<Finance>> getSesFinanceData() {
        // 按统计时间称分组
        return DataUtil.getFinanceData().stream()
                .collect(Collectors.groupingBy(Finance::getSes));
    }

    @Override
    public List<TotalOperatingRevenue> getTotalOperatingRevenueList() {
        return afterRebuild(FinanceDataUtils.getTotalOperatingRevenueList());
    }

    @Override
    public List<Roe> getRoeList() {
        return afterRebuild(FinanceDataUtils.getRoeList());
    }

    @Override
    public List<AdjustedProfit> getAdjustedProfitList() {
        return afterRebuild(FinanceDataUtils.getAdjustedProfitList());
    }

    @Override
    public List<NetProfitMargin> getNetProfitMarginList() {
        return afterRebuild(FinanceDataUtils.getNetProfitMarginList());
    }

    @Override
    public List<GrossProfitMargin> getGrossProfitMarginList() {
        return afterRebuild(FinanceDataUtils.getGrossProfitMarginList());
    }

    @Override
    public List<LiabilityOnAssetRatio> getLiabilityOnAssetRatioList() {
        return afterRebuild(FinanceDataUtils.getLiabilityOnAssetRatioList());
    }

    @Override
    public List<IncTotalRevenueYearOnYear> getIncTotalRevenueYearOnYearList() {
        return afterRebuild(FinanceDataUtils.getIncTotalRevenueYearOnYearList());
    }

    private <T extends FinanceYearColumn> List<T> afterRebuild(List<T> list) {
        list.forEach(item -> {
            Wugu rawWugu = wuguService.getRawWugu(item.getStock());
            if (rawWugu != null) {
                item.setIndustry(rawWugu.getIndustry());
                item.setWugu(rawWugu.getWugu());
            }
        });
        return list;
    }
}
